package com.vipheyue.huaxue.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.vipheyue.huaxue.R;
import com.vipheyue.huaxue.bean.MessageBean;
import com.vipheyue.huaxue.bean.MyUser;
import com.vipheyue.huaxue.view.CommonAdapter;
import com.vipheyue.huaxue.view.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;


public class HavedChatActivity extends AppCompatActivity {
    @Bind(R.id.tv_Menu_Title)
    TextView tv_Menu_Title;
    @Bind(R.id.pull_refresh_list)
    PullToRefreshListView pull_refresh_list;
    /*  * 数据中心
    */
    private CommonAdapter<MessageBean> mAdapter;
    private List<MessageBean> mDatas = new ArrayList<MessageBean>();
    private int nowPageNum = 0;
    private int pageSize = 50;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_haved_chat);
        ButterKnife.bind(HavedChatActivity.this);
        tv_Menu_Title.setText("我的发布");
        mDatas.clear();
        initRefresh();
        setAdapter();
        initData(nowPageNum);
    }

    private void initData(int getNumPage) {
        MyUser user = BmobUser.getCurrentUser(this, MyUser.class);

        BmobQuery<MessageBean> eq1 = new BmobQuery<MessageBean>();
        eq1.addWhereEqualTo("releaseUser", user);
        eq1.include("username");
        BmobQuery<MessageBean> eq2 = new BmobQuery<MessageBean>();
        eq2.addWhereEqualTo("tendUser", user);
        eq2.include("username");
        List<BmobQuery<MessageBean>> queries = new ArrayList<BmobQuery<MessageBean>>();
        queries.add(eq1);
        queries.add(eq2);
        BmobQuery<MessageBean> mainQuery = new BmobQuery<MessageBean>();
        mainQuery.or(queries);
        mainQuery.findObjects(this, new FindListener<MessageBean>() {
            @Override
            public void onSuccess(List<MessageBean> object) {
                mDatas.addAll(object);
                nowPageNum = nowPageNum + 1;
                mAdapter.notifyDataSetChanged();
                // Call onRefreshComplete when the list has been refreshed.
                if (pull_refresh_list != null) {
                    pull_refresh_list.onRefreshComplete();
                }

            }
            @Override
            public void onError(int code, String msg) {
                if (pull_refresh_list != null) {
                    pull_refresh_list.onRefreshComplete();
                }

            }
        });
    }


    private void initRefresh() {
        pull_refresh_list.setMode(PullToRefreshBase.Mode.BOTH);
        pull_refresh_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                String label = DateUtils.formatDateTime(HavedChatActivity.this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
                mDatas.clear();
                nowPageNum = 0;
                initData(nowPageNum);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                String label = DateUtils.formatDateTime(HavedChatActivity.this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
                // Update the LastUpdatedLabel
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
                // Do work to refresh the list here.
                initData(nowPageNum);
            }

        });
    }

    private void setAdapter() {
        //mdatas 记得加数据进去
        mAdapter = new CommonAdapter<MessageBean>(HavedChatActivity.this, mDatas, R.layout.job_item) {
            @Override
            public void convert(ViewHolder helper, int position, MessageBean item) {
                Log.d("xxxxc", new Gson().toJson(item));
//                helper.setText(R.id.tv_fragment0Item_name, item.getReleaseUser().getObjectId() + "   " + item.getTendUser().getObjectId());
            }
        };
        pull_refresh_list.setOnItemClickListener(new ItemClickListener());
        pull_refresh_list.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
    }

    @OnClick(R.id.rl_menu_left)
    public void onClick() {
        finish();
    }


    private final class ItemClickListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//            int REposition=position-1;//加了刷新位置变了
//            ListView listView = (ListView) parent;
//            MessageBean job = (MessageBean) listView.getItemAtPosition(position);
//            String jobStr = GsonUtils.toJson(job);
//            Intent intent = new Intent(HavedChatActivity.this, ChooseTenderActivity.class);
//            intent.putExtra("jobStr", jobStr);
//            startActivity(intent);
        }
    }


}
